package com.horoscope.astrology.zodiac.signs.free

import android.app.Activity
import android.app.Application
import android.os.Bundle
import com.adjust.sdk.Adjust
import com.adjust.sdk.AdjustConfig
import com.horoscope.astrology.zodiac.signs.free.utils.EventType
import com.horoscope.astrology.zodiac.signs.free.utils.FlurryEventUtil
import com.horoscope.astrology.zodiac.signs.free.utils.SysUtils
import com.horoscope.astrology.zodiac.signs.free.utils.ad.AudienceNetworkInitializeHelper
import java.io.InputStream

class MmoApplication : Application() {

    private var mHostIp: String? = null
    private var mChannel: String = ""

    override fun onCreate() {
        super.onCreate()
        initAudienceNetworkAd()
        mInstance = this
        setHostIP()
        setChannel()
        appStartCount()
        initAdjust()
    }

    // FaceBook ad 初始化
    private fun initAudienceNetworkAd() {
        AudienceNetworkInitializeHelper.initialize(this)
    }

    private fun appStartCount() {
        val count = AppSettingManager.appOpenCount()
        if (count <= 30) {
            when (count) {
                2 -> {
                    FlurryEventUtil.putDataToEvent(EventType.TYPE_SECOND_OPEN, null, null)
                }
                5 -> {
                    FlurryEventUtil.putDataToEvent(EventType.TYPE_FIVE_OPEN, null, null)
                }
                7 -> {
                    FlurryEventUtil.putDataToEvent(EventType.TYPE_SEVEN_OPEN, null, null)
                }
                15 -> {
                    FlurryEventUtil.putDataToEvent(EventType.TYPE_FIFTEEN_OPEN, null, null)
                }
                30 -> {
                    FlurryEventUtil.putDataToEvent(EventType.TYPE_THIRTY_OPEN, null, null)
                }
            }
        }
    }

    // 主要是广告收入统计跟踪
    private fun initAdjust() {
        val appToken = getString(R.string.Token_Adjust)
        val environment = AdjustConfig.ENVIRONMENT_PRODUCTION
        //val environment = AdjustConfig.ENVIRONMENT_SANDBOX // test
        val config = AdjustConfig(this, appToken, environment)
        Adjust.onCreate(config)
        registerActivityLifecycleCallbacks(adjustLifecycleCallbacks)
    }

    private fun setChannel() {
        mChannel = AppMetaDataParser.getChannel(this@MmoApplication)
    }

    private fun setHostIP() {
        mHostIp = AppMetaDataParser.getHostIP(this@MmoApplication)
    }

    fun getHostIP(): String? = mHostIp

    fun getAdvertisingId(): String {
        val udid = AppSettingManager.readString("deviceid", "")
        // 第一次生成UDID并储存
        if (udid.isBlank()) {
            mDeviceId = SysUtils.getDeviceUniqueID(this)
            AppSettingManager.saveString(mDeviceId, "deviceid")// 保存
        } else {
            mDeviceId = udid
        }
        return mDeviceId
    }

    fun getCertificateInputStream(): InputStream {
        return if (BuildConfig.DEBUG) {
            assets.open("development.crt")
        } else {
            assets.open("appfloodcom.crt")
        }
    }


    companion object {
        private var mInstance: MmoApplication = MmoApplication()
        private var mDeviceId: String = ""
        fun getInstance(): MmoApplication = mInstance
    }

    private val adjustLifecycleCallbacks = object : ActivityLifecycleCallbacks {
        override fun onActivityStarted(activity: Activity?) {
        }

        override fun onActivityDestroyed(activity: Activity?) {
        }

        override fun onActivitySaveInstanceState(activity: Activity?, outState: Bundle?) {
        }

        override fun onActivityStopped(activity: Activity?) {
        }

        override fun onActivityCreated(activity: Activity?, savedInstanceState: Bundle?) {
        }

        override fun onActivityPaused(activity: Activity?) {
            Adjust.onPause()
        }

        override fun onActivityResumed(activity: Activity?) {
            Adjust.onResume()
        }

    }

}